﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class Employee: IEmployee
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public Employee(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.Employee GetEmployee(System.Int32 employeeID)
		{
			BEGDC.PreContract.DAL.Employee employee = BEGDC.PreContract.DAL.Employee.TryFind(employeeID);

            return employee;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetEmployee_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM Employee";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteEmployee(System.Int32 employeeID)
		{
			try
            {
				BEGDC.PreContract.DAL.Employee employee = BEGDC.PreContract.DAL.Employee.TryFind(employeeID);
                employee.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Employee", string.Format("Error occured when deleting the Employee, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddEmployee(System.String badge, System.String eName, System.String cName, System.String email, System.String cILAccount, System.String bD, System.String bG, System.String title, System.String culture, System.String reportTo, System.Boolean active, Nullables.NullableDateTime updateDate, System.Boolean commentAllowed, System.Int32 aSBOrLucent, System.Boolean downloadAllowed, System.String phone, System.String company, System.String location, System.String aDAccount)	
		{
			try
            {
				BEGDC.PreContract.DAL.Employee employee = new BEGDC.PreContract.DAL.Employee();
				
				employee.Badge = badge;
				employee.EName = eName;
				employee.CName = cName;
				employee.Email = email;
				employee.CILAccount = cILAccount;
				employee.BD = bD;
				employee.BG = bG;
				employee.Title = title;
				employee.Culture = culture;
				employee.ReportTo = reportTo;
				employee.Active = active;
				employee.UpdateDate = updateDate;
				employee.CommentAllowed = commentAllowed;
				employee.ASBOrLucent = aSBOrLucent;
				employee.DownloadAllowed = downloadAllowed;
				employee.Phone = phone;
				employee.Company = company;
				employee.Location = location;
				employee.ADAccount = aDAccount;
				employee.CreateAndFlush();
				
				return employee.EmployeeID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Employee", string.Format("Error occured when adding a new Employee, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateEmployee(System.Int32 employeeID, System.String badge, System.String eName, System.String cName, System.String email, System.String cILAccount, System.String bD, System.String bG, System.String title, System.String culture, System.String reportTo, System.Boolean active, Nullables.NullableDateTime updateDate, System.Boolean commentAllowed, System.Int32 aSBOrLucent, System.Boolean downloadAllowed, System.String phone, System.String company, System.String location, System.String aDAccount)
		{
			try
            {
                BEGDC.PreContract.DAL.Employee employee = GetEmployee(employeeID);

				employee.Badge = badge;
				employee.EName = eName;
				employee.CName = cName;
				employee.Email = email;
				employee.CILAccount = cILAccount;
				employee.BD = bD;
				employee.BG = bG;
				employee.Title = title;
				employee.Culture = culture;
				employee.ReportTo = reportTo;
				employee.Active = active;
				employee.UpdateDate = updateDate;
				employee.CommentAllowed = commentAllowed;
				employee.ASBOrLucent = aSBOrLucent;
				employee.DownloadAllowed = downloadAllowed;
				employee.Phone = phone;
				employee.Company = company;
				employee.Location = location;
				employee.ADAccount = aDAccount;
				employee.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Employee", string.Format("Error occured when updating the Employee, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



